<?php
/*
	申请成为代理商
	如果已申请，但尚未审核通过，则页面显示请耐心等待管理员审核！
	如果已申请，且审核通过，则显示审核通过，并提供按钮前往代理中心
	如果审核不通过，则显示审核不通过原因
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
if($_RQ['op']=='main'){
	set_html('title','完善代理商信息');
	$member=$this->pdo_get('member',array('merchid'=>$_SESSION['onemerchid'],'mid'=>$_SESSION['uid']));
	if($member['agent_id']){
		$return['agent']=model('core/member')->detail($member['agent_id'],array('nickname','avatar'));
		$return['agent_name']=$return['agent']['nickname'];
	}
	$setting=$this->plugin_setting('page');
	$return['setting']=$setting;
	if($_SESSION['onemerchid']){
		$return['applyset']=$this->plugin_setting('apply'.$_SESSION['onemerchid']);
	}else{
		$return['applyset']=$this->plugin_setting('apply');
	}
	$getinfo=model('core/member')->detail($_SESSION['uid'],array('nickname','avatar','realname','mobile','wechat'));
	$member['realname']=$member['realname']?:$getinfo['realname'];
	$member['mobile']=$member['mobile']?:$getinfo['mobile'];
	$member['wechat']=$member['wechat']?:$getinfo['wechat'];
	$member['diyformfields']=$setting['diyformfields']?:array();
	$return['member']=$member;
	exi($return);
}
if($_RQ['op']=='apply'){
	$post=get_postdata();
	if(!$post['agree']){
		exi('请先阅读协议！','error');
	}
	if(!$post['realname']){
		exi('请输入姓名！','error');
	}
	if(!$post['mobile']){
		exi('请输入联系电话！','error');
	}
	$member_update=array(
		'realname'=>trim($post['realname']),
		'mobile'=>trim($post['mobile']),
		'diyformdata'=>ijson_encode($post['diyformfields']),
	);
	if($post['wechat']){
		$member_update['wechat']=trim($post['wechat']);
	}
	$this->model('member')->member_update($_SESSION['uid'],$member_update);	
	exi('完成信息成功！','','/index/');
}